package com.zzh.haodai.myseekbar.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by haodai on 17/5/3.
 */

public class SeekBarView extends View{
    private static final String TAG = SeekBarView.class.getSimpleName();

    public interface OnSeekBarListener{
        void valueX(int vlaueX);
    }

    private OnSeekBarListener onSeekBarListener;

    public void setOnSeekBarListener(OnSeekBarListener onSeekBarListener) {
        this.onSeekBarListener = onSeekBarListener;
    }

    //底部的线
    private Paint mBottomLine;
    //控件的长度
    private int mWidth;
    //控件的高度
    private int mHeight;
    //左右间隔距离
    private int mLeftPadding = 20;
    private int mRightPadding = 20;
    //线的宽度
    private int mHeightLine = 20;
    //底线的颜色
    private int mBottomLineColor = Color.GRAY;
    //移动浮标绘笔
    private Paint mFloatPaint ;
    //移动浮标颜色
    private int mFloatPaintColor = Color.RED;
    //浮动点间距 暂定为两个圆的半径和的2倍
    private int mFloatSpacing;
    //大圆的半径 暂定为小圆的二倍
    private int mCircleRadius;
    //变动的值
    private int mValue = 0;
    //绘制浮点path
    private Path mFloatPath;
    //可用区间最大值
    private int mMaxWidth;
    //可用区间最小值
    private int mMinWidth;
    //value总值
    private int mMaxValue = 100;
    //value区间值
    private double mInterval;


    public SeekBarView(Context context) {
        this(context,null);
    }

    public SeekBarView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SeekBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * 初始化数据
     */
    private void init(){
        this.mBottomLine = new Paint();
        this.mBottomLine.setColor(mBottomLineColor);
        this.mBottomLine.setAntiAlias(true);

        this.mFloatPaint = new Paint();
        this.mFloatPaint.setAntiAlias(true);
        this.mFloatPaint.setColor(mFloatPaintColor);

        this.mCircleRadius = mHeightLine;
        this.mFloatSpacing = 2 * (mHeightLine + mHeightLine / 2);

        this.mFloatPath = new Path();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        this.mHeight = getHeight();
        this.mWidth = getWidth();

        this.mMaxWidth = mWidth - mRightPadding - mFloatSpacing - mCircleRadius / 2;
        this.mMinWidth = mLeftPadding;
        this.mInterval = (mMaxWidth - mMinWidth) / (double)mMaxValue;
        Log.i(TAG,">>mInterval>>>>>>" + mInterval);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBottomLine(canvas);
        drawFloat(canvas);
        drawMatrix(canvas);
        drawSelect(canvas);
    }

    /**
     * 绘制底线
     * @param canvas
     */
    private void drawBottomLine(Canvas canvas){
        mBottomLine.setStrokeWidth(mHeightLine);
        canvas.drawLine(mLeftPadding, mHeight / 2, mWidth - mRightPadding, mHeight / 2, mBottomLine);
        canvas.drawCircle(mLeftPadding, mHeight / 2, mHeightLine / 2,mBottomLine);
        canvas.drawCircle(mWidth - mRightPadding, mHeight / 2, mHeightLine / 2,mBottomLine);
    }

    /**
     * 绘制浮标
     */
    private void drawFloat(Canvas canvas){
        canvas.drawCircle(mLeftPadding, mHeight / 2, mHeightLine / 2,mFloatPaint);
        canvas.drawCircle(mValue + mLeftPadding + mFloatSpacing, mHeight / 2, mCircleRadius,mFloatPaint);
    }

    /**
     * 绘制矩阵区域
     * @param canvas
     */
    private int mFloatLeft;
    private int mFloatRight;
    private int mFloatTop;
    private int mFloatBottom;

    private void drawMatrix(Canvas canvas){
        this.mFloatLeft = mValue + mLeftPadding;
        this.mFloatRight = mValue + mLeftPadding + mFloatSpacing;
        this.mFloatTop = mHeight / 2 - mCircleRadius;
        this.mFloatBottom = mHeight / 2 + mCircleRadius;
        mFloatPath = new Path();
        mFloatPath.moveTo(this.mFloatLeft , mHeight / 2 + mHeightLine / 2);
        mFloatPath.lineTo(this.mFloatRight , this.mFloatBottom);
        mFloatPath.lineTo(this.mFloatRight , this.mFloatTop);
        mFloatPath.lineTo(this.mFloatLeft , mHeight / 2 - mHeightLine /2);
        mFloatPaint.setStyle(Paint.Style.FILL);
        canvas.drawPath(mFloatPath,mFloatPaint);
    }

    //绘制已选择区域
    private void drawSelect(Canvas canvas){
        mFloatPaint.setStrokeWidth(mHeightLine);
        mFloatPaint.setAntiAlias(true);
        canvas.drawLine(mLeftPadding, mHeight / 2, mValue + mHeightLine, mHeight / 2, mFloatPaint);
    }


    private boolean isEffect = false;
    private float mDownX;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                this.mDownX = event.getX();
                if(this.mDownX > this.mFloatLeft &&
                        this.mDownX < this.mFloatRight &&
                        event.getY() > this.mFloatTop &&
                        event.getY() < this.mFloatBottom)
                    this.isEffect = true;
                else this.isEffect = false;
                Log.i(TAG,">>>ACTION_DOWN>>>>" + event.getX());
                break;
            case MotionEvent.ACTION_UP:
                Log.i(TAG,">>>ACTION_UP>>>>" + event.getX());
                break;
            case MotionEvent.ACTION_MOVE:
                if(!this.isEffect) return true;
                this.mValue = (int)event.getX() - mMinWidth;
                if(this.mValue <= 0)
                    this.mValue = 0;
                if(event.getX() >= mMaxWidth)
                    this.mValue = mMaxWidth - mMinWidth;
                invalidate();
                if(onSeekBarListener != null)
                    onSeekBarListener.valueX((int)(this.mValue / mInterval));
                break;
            default:
                break;
        }

        return true;
    }

}
